package IT.DataAccess;

import IT.Entities.Area;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;


public class DatArea implements IDatArea{
       
    private Area ent;
    private List<Area> oLista;
    private BDConexion oBase;
    
    private Connection oConexion;
    private CallableStatement oCst;    
    private ResultSet oRst;    
     
    public List<Area> Listar() {            
        oLista = new ArrayList<Area>();
        try {
            oBase = new BDConexion();
            oConexion = oBase.getDataSource().getConnection();
            oBase = null;
            oCst = oConexion.prepareCall("{call usp_AreaListar()}");
            oRst = oCst.executeQuery();

            while (oRst.next()) {
                oLista.add(new Area(oRst.getInt("cod_ar_in"), oRst.getString("nom_ar_vc"),
                        oRst.getString("des_ar_vc"), oRst.getInt("est_ar_in"), oRst.getString("Estado")));
            }
            oRst.close();
            oCst.close();
            oConexion.close();
        } catch (SQLException ex) {
            Logger.getLogger(DatArea.class.getName()).log(Level.SEVERE, null, ex);
        }

        return oLista;       
    }
    
    public Area Ind(int nCodigo) {
        ent = new Area();
        try {
            oBase = new BDConexion();
            oConexion = oBase.getDataSource().getConnection();
            oBase = null;
            oCst = oConexion.prepareCall("{call usp_AreaListar(?)}");
            oCst.setInt(1, nCodigo);
            oRst = oCst.executeQuery();

            while (oRst.next()) {
                ent.setCod_ar_in(oRst.getInt("cod_ar_in"));
                ent.setNom_ar_vc(oRst.getString("nom_ar_vc"));
                ent.setDes_ar_vc(oRst.getString("des_ar_vc"));
                ent.setEst_ar_in(oRst.getInt("est_ar_in"));
                ent.setEstado(oRst.getString("Estado"));                
            }
            oRst.close();
            oCst.close();
            oConexion.close();
        } catch (SQLException ex) {
            Logger.getLogger(DatArea.class.getName()).log(Level.SEVERE, null, ex);
        }
      
        return ent;
    }
    
    public void Insertar(Area ent) {        
        try {
            oBase = new BDConexion();
            oConexion = oBase.getDataSource().getConnection();
            oBase = null;
            oCst = oConexion.prepareCall("{call usp_AreaInsertar(?,?,?)}");              
            oCst.setString(1, ent.getNom_ar_vc());
            oCst.setString(2, ent.getDes_ar_vc());
            oCst.setInt(3, ent.getEst_ar_in());
            oCst.execute();
            oCst.close();
            oConexion.close();
        } catch (SQLException ex) {
            Logger.getLogger(DatArea.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void Editar(Area ent) {
        try {
            oBase = new BDConexion();
            oConexion = oBase.getDataSource().getConnection();
            oBase = null;
            oCst = oConexion.prepareCall("{call usp_AreaEditar(?,?,?,?)}");
            oCst.setInt(1, ent.getCod_ar_in());           
            oCst.setString(2, ent.getNom_ar_vc());
            oCst.setString(3, ent.getDes_ar_vc());
            oCst.setInt(4, ent.getEst_ar_in());
            oCst.execute();
            oCst.close();
            oConexion.close();
        } catch (SQLException ex) {
            Logger.getLogger(DatArea.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public int Eliminar(int nCodigo) {
        int nResultado=0;
        try {
            oBase = new BDConexion();
            oConexion = oBase.getDataSource().getConnection();
            oBase = null;
            oCst = oConexion.prepareCall("{call usp_AreaEliminar(?,?)}");
            oCst.setInt(1, nCodigo);      
            oCst.registerOutParameter(2, java.sql.Types.INTEGER);
            oCst.execute();
            nResultado=oCst.getInt(2);
            oCst.close();
            oConexion.close();
        } catch (SQLException ex) {
            //nResultado=1;
            Logger.getLogger(DatArea.class.getName()).log(Level.SEVERE, null, ex);
        }
        return nResultado;
    }

    public int Valida(int nCodigo, String cArea) {
        int nValida=0;        
        try {
            oBase = new BDConexion();
            oConexion = oBase.getDataSource().getConnection();
            oBase = null;
            oCst = oConexion.prepareCall("{call usp_AreaValida(?,?,?)}");              
            oCst.setInt(1, nCodigo);
            oCst.setString(2, cArea);
            oCst.registerOutParameter(3, java.sql.Types.INTEGER);
            oCst.execute();
            nValida=oCst.getInt(3);
            oCst.close();
            oConexion.close();
        } catch (SQLException ex) {
            Logger.getLogger(DatArea.class.getName()).log(Level.SEVERE, null, ex);
        }
        return nValida;
    }
}
